package com.example.planetexploder.module.picture;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.ImageView;

import com.example.planetexploder.R;
import com.example.planetexploder.bean.maindata.BookInfo;
import com.example.planetexploder.bean.maindata.ImgInfo;
import com.example.planetexploder.bean.maindata.ItemInfo;
import com.example.planetexploder.bean.otherdata.CutRatio;
import com.example.planetexploder.utils.IdCreator;
import com.example.planetexploder.utils.screenshot.MyRect;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class View_ImgProcess extends AppCompatActivity {

    private static final String TAG = "View_ImgProcess";

    private BookInfo bookInfo;
    private ItemInfo itemInfo;
    private ImgInfo imgInfo;

    private Presenter_ImgProcess presenterImg;

    private ImageView iv_pic;
    private File displayFile;
    private Bitmap display;
    private Bitmap cutted;

    //截图工具
    private MyRect myRect;
    private ImageView iv_p1;
    private ImageView iv_p2;
    private ImageView iv_p3;
    private ImageView iv_p4;

    //颜色转换后的图片
    private Bitmap bitmap_change;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_img_cut);
        requestPermissions(new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        File dir = getFilesDir();

        iv_pic = findViewById(R.id.iv_pic);
        iv_p1 = findViewById(R.id.iv_p1);
        iv_p2 = findViewById(R.id.iv_p2);
        iv_p3 = findViewById(R.id.iv_p3);
        iv_p4 = findViewById(R.id.iv_p4);

        Intent intent0 = getIntent();
        itemInfo = (ItemInfo) intent0.getSerializableExtra("ItemInfo");
        bookInfo = (BookInfo) intent0.getSerializableExtra("BookInfo");
        imgInfo = (ImgInfo) intent0.getSerializableExtra("ImgInfo");


        if (imgInfo.imgId == null) {
            imgInfo.imgId = IdCreator.create_ImgId();
            presenterImg = new Presenter_ImgProcess(getApplicationContext(), bookInfo, itemInfo, imgInfo);
            presenterImg.addImgToDao();

            displayFile = presenterImg.getImgFile();

            //启动相机并把相片储存至本地
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            Uri uri = FileProvider.getUriForFile(getApplicationContext(), "com.example.planetexploder.provider", displayFile);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            startActivityForResult(intent, 1);
        }
        else {
            presenterImg = new Presenter_ImgProcess(getApplicationContext(), bookInfo, itemInfo, imgInfo);
            displayFile = presenterImg.getImgFile();
        }

        Button bt_raw = findViewById(R.id.bt_raw);

        //创建输入流和展示的bitmap
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(displayFile.getAbsoluteFile());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        display = BitmapFactory.decodeStream(fis);

        //点击四个按钮时的操作
        bt_raw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {  //原图
            iv_pic.setImageBitmap(display);
            }
        });

        Button bt_white = findViewById(R.id.bt_white);
        bt_white.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {  //黑白

            }
        });

        Button bt_confirm = findViewById(R.id.bt_confirm);
        bt_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {  //确认保存图片
                //裁剪图片
                CutRatio cr = myRect.getCutRatio();
                int w = display.getWidth();
                int h = display.getHeight();
//                Log.d(TAG, cr.rx1 + " " + cr.rx2 + " " + cr.ry1 + " " + cr.ry2);
                cutted = Bitmap.createBitmap(display,(int)(w*cr.rx1),(int)(h*cr.ry1),(int)(w*(cr.rx2-cr.rx1)),(int)(h*(cr.ry2-cr.ry1)),null,false);
                //保存图片
                presenterImg.saveImage(cutted);
                finish();
//                Log.d(TAG, "储存成功");
            }
        });

        Button bt_cancel = findViewById(R.id.bt_cancel);
        bt_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {  //重拍
                Intent intent_restart = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                Uri uri_restart = FileProvider.getUriForFile(getApplicationContext(), "com.example.planetexploder.provider", displayFile);
                intent_restart.putExtra(MediaStore.EXTRA_OUTPUT, uri_restart);
                startActivityForResult(intent_restart, 1);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 1)
        {
            if (resultCode == Activity.RESULT_OK) {
                try {
                    FileInputStream fis = new FileInputStream(displayFile.getAbsoluteFile());
                    display = BitmapFactory.decodeStream(fis);
                    iv_pic.setImageBitmap(display);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

                myRect = new MyRect(iv_pic, iv_p1, iv_p2, iv_p3, iv_p4);

                ViewTreeObserver vto = iv_p4.getViewTreeObserver();
                vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                    boolean flag = true;
                    @Override
                    public boolean onPreDraw() {
                        if (flag) {
                            myRect.setMeasure();
                            flag = false;
                        }
                        return true;
                    }
                });

            }
        }
    }
}
